package org.example.medical.controller;

import org.example.medical.entity.DataSharing;
import org.example.medical.service.DataSharingService;
import org.example.medical.utils.ApiResult;
import org.example.medical.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/dataSharings")
public class DataSharingApiController {
    @Autowired
    private DataSharingService dataSharingService;

    // 1. 分页查询接口
    @GetMapping
    public ApiResult<PageResult<DataSharing>> listDataSharings(
            @RequestParam(required = false) String hospital,
            @RequestParam(required = false) String department,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size
    ) {
        List<DataSharing> records = dataSharingService.getDataSharings(hospital, department, page, size);
        Long total = (long) records.size();
        return ApiResult.success(new PageResult<>(records, total, page, size));
    }

    // 2. 新增数据共享记录
    @PostMapping
    public ApiResult<?> addDataSharing(@RequestBody DataSharing dataSharing) {
        dataSharingService.addDataSharing(dataSharing);
        return ApiResult.success(null, "Data sharing record added successfully");
    }

    // 3. 更新数据共享记录
    @PutMapping("/{shareId}")
    public ApiResult<?> updateDataSharing(
            @PathVariable Long shareId,
            @RequestBody DataSharing dataSharing
    ) {
        dataSharing.setShareId(shareId);
        dataSharingService.updateDataSharing(dataSharing);
        return ApiResult.success(null, "Data sharing record updated successfully");
    }

    // 4. 软删除数据共享记录
    @DeleteMapping("/{shareId}")
    public ApiResult<?> softDeleteDataSharing(@PathVariable Long shareId) {
        dataSharingService.softDeleteDataSharing(shareId);
        return ApiResult.success(null, "Data sharing record deleted successfully");
    }

    // 5. 批量软删除数据共享记录
    @PostMapping("/batchDelete")
    public ApiResult<?> batchSoftDeleteDataSharings(@RequestBody List<Long> shareIds) {
        dataSharingService.batchSoftDeleteDataSharings(shareIds);
        return ApiResult.success(null, "Data sharing records deleted successfully");
    }
}